<template>
  <a-modal
    ok-text="确认"
    cancel-text="取消"
    :maskClosable="false"
    centered
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    footer=""
    @cancel="handCancel"
  >
    <div id="qrCode">
      <vue-qr
        :text="text"
        :size="300"
        :margin="0"
        colorDark="black"
        colorLight="white"
      ></vue-qr>
      <div class="labelBox" v-if="label !== ''">{{ label }}</div>
    </div>
    <div class="modal-footer">
      <a-button type="primary" @click="getQrCodeImage()">下载</a-button>
      <a-button @click="handCancel">取消</a-button>
    </div>
  </a-modal>
</template>

<script>
import VueQr from "vue-qr";
export default {
  data() {
    return {
      title: "",
      visible: false,
      text: "",
      label: "",
    };
  },
  methods: {
    showModal(text, title) {
      this.title = title;
      this.text = text;
      let label = JSON.parse(text).label;
      if (label) {
        this.label = label;
      }
      this.visible = true;
    },
    getQrCodeImage() {
      var canvasData = document
        .getElementById("qrCode")
        .getElementsByTagName("img");
      var a = document.createElement("a");
      a.href = canvasData[0].src;
      a.download = `${this.title}.png`;
      a.click();
    },
    handCancel() {
      this.visible = false;
    },
  },
  components: {
    VueQr,
  },
};
</script>

<style lang="less" scoped>
#qrCode {
  text-align: center;
}
.modal-footer {
  text-align: center;
  margin: 20px 0 0 0;
  button {
    margin: 0 20px;
  }
}
.labelBox {
  text-align: center;
  font-size: 24px;
  margin: 20px 0;
}
</style>
